<?php

/**
 * @version $Id: main.php 2009-04-24
 * @package DGC.CMS
 * @author DGC Dev <dev@dgc.vn>
 * @copyright Copyright (C) 2008 - 2009 by DGC Corp. All rights reserved.
 * @link http://www.dgc.vn
 */

defined("DGC_PHP") or die("Application is stopping!!!");
 
$app = dgcRegistry::get('app');
$app->setMenu('cost');
$id = dgcGetParam($_REQUEST, 'task',0);
  
$page = dgcGetParam($_REQUEST, 'm', 0); 

if($id > 0)
    costDetail($id);
else
    Showcost ();

function Showcost(){

    $hdb = dgcRegistry::get('db');
    $app = dgcRegistry::get('app');
    $template = dgcRegistry::get('template');
   
    $lang = dgcRegistry::get('lang');
    $language_id = $lang[$app->getLang()];
    
   $qr ='SELECT * 
        FROM cost,cost_desc
        WHERE language_id = '.$language_id.'
        AND published = 1
        AND id = cost_id 
        ORDER BY ordering DESC
        ';
    $hdb->setQuery($qr);  
    
    $costs = $hdb->loadObjectList();
    
    
    //paging
    $item_total = count($costs);  
   
    $page_record_max = 10;
    $curPage = dgcGetParam($_REQUEST, 'p', 1);
    if($curPage < 1)
        $curPage = 1;
    $url = sprintf("%s/cost/", DGC_WEB_PATH);
    $pagestring = '?p=';
    $paging = new dgcPaging($item_total, $curPage, $url, $pagestring, '', $page_record_max);
    $paging->setCurPage($curPage);
    $limit = sprintf("\n LIMIT %d, %d", ($paging->getCurPage() - 1) * $page_record_max, $page_record_max);
    //end paging   
    
     $qr ="SELECT * 
        FROM cost,cost_desc
        WHERE language_id = $language_id
        AND published = 1
        AND id = cost_id 
        ORDER BY ordering DESC 
        $limit";
    $hdb->setQuery($qr);
    
    $costs = $hdb->loadObjectList();
    foreach ($costs as $cost) {
        if($cost->image == "")
            $cost->image = 'images/no_image.jpeg';
    }
    
    
    
    $app->setTitle('Lĩnh vực hoạt động');
    
    $template->assign(array(
        'paging'                => $paging->showDivPages(),
        'header_cost'    => $lang['header_cost'],
        'cost'           => $lang['cost'],
        'costs'          => $costs,
        'web_path'        => DGC_WEB_DOMAIN,
    ));
    
   
}

function costDetail($id) {

     
    $hdb = dgcRegistry::get('db');
    $app = dgcRegistry::get('app');
    $template = dgcRegistry::get('template');
    $app->setFile('detail');
    $lang = dgcRegistry::get('lang');
    $language_id = $lang[$app->getLang()];
    
   //update view 
   $qr ='SELECT * 
        FROM cost,cost_desc
        WHERE id = ' . $id . '
        AND language_id = '.$language_id.'
        AND published = 1
        AND id = cost_id';
    $hdb->setQuery($qr);
    
    $costs = $hdb->loadObjectList();
    $view = $costs[0]->view + 1; 
    
    $app->setTitle($costs[0]->name);
    
   
   
  
    //echo'<pre>';var_dump($qr);die();
   
    
    //get cost detail
     $qr = 'SELECT *
           FROM cost_desc,cost
           WHERE language_id = '.$language_id.'   
           AND cost_desc.cost_id = '. $id;
    $hdb->setQuery($qr);
    $cost_detail = $hdb->loadObjectList();
    $cost_detail = $cost_detail[0];
	
	$arr = explode("/",$cost_detail->image);
	$cost_detail->face = $arr[count($arr)-1];
      //get other cost
    $qr = 'SELECT *
           FROM cost,cost_desc
           WHERE cost.id = cost_desc.cost_id and published = 1
           AND cost_desc.language_id = ' . $language_id.'
           AND cost.id <> ' . $id.'
           ORDER BY cost.ordering DESC
           LIMIT 0,5';
    $hdb->setQuery($qr);
    $cost_same_type = $hdb->loadObjectList();
    $item_total = count($cost_same_type);
    $template->assign(array(
        'item_total'                => $item_total,
         'cost_same_type'       => $cost_same_type,
       'other_cost'            => $lang['other_cost'], 
        'header_cost'    => $lang['header_cost'],
       
        'cost_detail'          =>  $cost_detail ,
        
        'cost'                 => $lang['cost'],
        'costs'                => $costs,
        'web_path'             => DGC_WEB_DOMAIN,
    ));
    

    //update so nguoi xem
   $qr ='UPDATE cost
        SET view = '.$view.'
        WHERE id = ' . $id;
    $hdb->setQuery($qr);
    
    $costs = $hdb->query($qr);
}

